/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.client.view;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import com.agenda.client.listeners.UsuariosSelecionadosListener;
import com.agenda.model.Usuario;

/**
 *
 * @author Murilo
 */
public class UsuariosListDialogView extends javax.swing.JDialog {

	/**
	 * Creates new form UsuariosListDialogView
	 */
	public UsuariosListDialogView(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public UsuariosListDialogView(java.awt.Dialog parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of
	 * this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jPanel2 = new javax.swing.JPanel();
		adicionarButton = new javax.swing.JButton();
		cancelarButton = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		usuariosJList = new javax.swing.JList();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
			.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 398, Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 57, Short.MAX_VALUE));

		getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

		jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

		adicionarButton.setText("Confirmar");
		adicionarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				adicionarButtonActionPerformed(evt);
			}
		});
		jPanel2.add(adicionarButton);

		cancelarButton.setText("Cancelar");
		cancelarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelarButtonActionPerformed(evt);
			}
		});
		jPanel2.add(cancelarButton);

		getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

		usuariosJList.setModel(new javax.swing.AbstractListModel() {
			String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jScrollPane1.setViewportView(usuariosJList);

		getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void adicionarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarButtonActionPerformed
		confirmar();
	}//GEN-LAST:event_adicionarButtonActionPerformed

	private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
		cancelar();
	}//GEN-LAST:event_cancelarButtonActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 * Set the Nimbus look and feel
		 */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(UsuariosListDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(UsuariosListDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(UsuariosListDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(UsuariosListDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/*
		 * Create and display the dialog
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				UsuariosListDialogView dialog = new UsuariosListDialogView(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {

					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton adicionarButton;
	private javax.swing.JButton cancelarButton;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JList usuariosJList;

	// End of variables declaration//GEN-END:variables

	private UsuariosSelecionadosListener usuariosSelecionadosListener;

	private void confirmar() {
		Object[] dadosSelecionados = usuariosJList.getSelectedValues();

		if (dadosSelecionados.length > 0) {
			List<Usuario> usuarios = new ArrayList<Usuario>();

			for (int i = 0; i < dadosSelecionados.length; i++)
				usuarios.add((Usuario) dadosSelecionados[i]);

			if (usuariosSelecionadosListener != null) {
				usuariosSelecionadosListener.selecionou(usuarios);
			}
			cancelar();
		} else {
			JOptionPane.showMessageDialog(this, "É necessário selecionar pelo menos um usuário.");
		}
	}

	private void cancelar() {
		dispose();
	}

	public void setUsuarios(List<Usuario> usuarios) {
		usuariosJList.setListData(usuarios.toArray());
	}

	public void setUsuariosListener(UsuariosSelecionadosListener usuariosSelecionadosListener) {
		this.usuariosSelecionadosListener = usuariosSelecionadosListener;
	}
}
